#include "../include/sheet.h"
#include "sheet_impl.h"

#include <file/include/file.h>
#include <common/include/memory.h>

excel::sheet* excel::load_sheet(const char* filename, const char* sheetname)
{
	io::ifile* sheetfile = io::read_file(filename);

	
	if (!sheetfile)
		return nullptr;

	int size = sheetfile->length();
	cmn::mem buffer = cmn::alloc_mem(size+4);
	memset(buffer, 0, size+1);


	sheetfile->read(buffer, size);
	sheetfile->close();
	sheetfile->release();
	wchar_t* string = (wchar_t*)buffer;

	if (string[0] != 0xfeff)//ucs-2 bom
	{
		buffer.free();
		return nullptr;
	}
	else
	{
		string[size / 2] = L'\r';
		string[size / 2+1] = L'\n';
		excel::sheet* s = create_sheet_from_mem((kcstr)buffer, size-4);
		buffer.free();
		return s;
	}
}